package cn.zhfeat.core.base;

import java.io.Serializable;
import java.util.List;

import cn.zhfeat.core.base.BaseDto;
import cn.zhfeat.core.base.interfaces.IBaseDao;
import cn.zhfeat.core.base.interfaces.IBaseService;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;



/**
 * @description: 底层实现类
 *
 * @author ZhangHao
 * @date 2018年4月26日 上午11:53:51 
 * @version 1.0.0.1
 */
public class BaseServiceImpl<E , D extends BaseDto, PK extends Serializable>  implements IBaseService<E , D ,PK> {


	@Autowired
	public IBaseDao<E , D , PK> baseDao;

	@Override
	public Integer insertSelective(E entity) {
		return baseDao.insertSelective(entity);
	}

	@Override
	public Integer insertGotEntityId(E entity) {
		return baseDao.insertGotEntityId(entity);
	}

	@Override
	public Integer insertGotEntityUuid(E entity) {
		return baseDao.insertGotEntityUuid(entity);
	}

	@Override
	public Integer batchInsert(List<E> list) {
		return baseDao.batchInsert(list);
	}

	@Override
	public Integer updateSelective(E entity) {
		return baseDao.updateSelective(entity);
	}

	@Override
	public Integer batchUpdate(List<E> list) {
		return baseDao.batchUpdate(list);
	}

	@Override
	public Integer deleteById(PK id) {
		return baseDao.deleteById(id);
	}

	@Override
	public Integer batchDelete(List<PK> list) {
		return baseDao.batchDelete(list);
	}

	@Override
	public <DTO> void deleteByCondition(DTO dto) {
		baseDao.deleteByCondition(dto);
	}

	@Override
	public E find(PK id) {
		return baseDao.selectByPrimaryKey(id);
	}

	@Override
	public E findByType(D dto) {
		return baseDao.findByType(dto);
	}

	@Override
	public List<E> findListByEntity(E entity) {
		return baseDao.findListByEntity(entity); 
	}
	
	@Override
	public List<E> findListByDto(D dto) {
		return baseDao.findListByDto(dto); 
	}

	@Override
	public <DTO> List<E> findGroupList(DTO dto) {
		return baseDao.findGroupList(dto);
	}

	@Override
	public <DTO> int count(DTO dto) {
		return baseDao.count(dto);
	}

	@Override
	public List<E> like(E entity) {
		return baseDao.like(entity);
	}

	@Override
	public Integer selectMaxId() {
		return baseDao.selectMaxId();
	}

	@Override
	public JSONObject jsonList(E entity) {
		JSONObject result = new JSONObject();
		List<E> list = baseDao.findListByEntity(entity);
		if (list != null && list.size() > 0) {
			result.put("status", "success");
			result.put("list", list);
			return result;
		} else {
			result.put("status", "error");
			result.put("msg", "error"); // 结果集为空
			return result;
		}
	}


	/**
	 * @description: 单纯返回分页对象。如果没有查询到要显示的对象则返回null
	 *
	 * @param dto
	 * @author ZhangHao
	 * @date 2018年4月27日 下午7:00:50 
	 * @version 1.0.0.1
	 */
	public PageInfo<E> pageList(D dto) {
		Integer startIndex = dto.getStartIndex(); 		// 当前第几页
		Integer pageSize = dto.getPageSize(); 				// 当前页所显示记录条数
		int num = 1;
		int size = 10;
		if (startIndex != null) {
			num = startIndex;
		}
		if (pageSize != null) {
			size = pageSize;
		}

		PageHelper.startPage(num, size);
		List<E> list = baseDao.queryPageByDto(dto); 
		if (list != null && list.size() > 0) {
			PageInfo<E> page = new PageInfo<E>(list);
			return page;
		} 
		return null;
	}

}
























